home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / wpjv1n5.zip / HELLO.ZIP / HELLO.C next >
C/C++ Source or Header  |  1993-05-02  |  17KB  |  576 lines

  1. /*============================================================================
  2.   Hello - Windows Programmer's Journal Volume 1 Number 5
  3.  
  4.   File      : Hello.C
  5.  
  6.   Prototype : 
  7.  
  8.   Call      : 
  9.  
  10.   Library   : 
  11.  
  12.   Example   : 
  13.  
  14.   24 April 1993 - dlcampbell
  15.   1993 Dave Campbell WynApse
  16. ----------------------------------------------------------------------------*/
  17. #include <windows.h>
  18. #include "hello.h"
  19. #include <string.h>
  20.  
  21. char       szAppName[] = "Hello";
  22. HANDLE     hInst;
  23. HWND       hWndMain;
  24. int        InitSettings;
  25. static     HMENU hMainMenu, hAlternateMenu;
  26.  
  27. static     char szFileName[128];
  28. static     char szFileSpec[16];
  29. static     char szDefExt[5];
  30. static     WORD wFileAttr;
  31. OFSTRUCT   of;
  32. LPOFSTRUCT pof;
  33.  
  34. /*----------------------------------------------------------------------------
  35.   Function Prototypes
  36. ----------------------------------------------------------------------------*/
  37. BOOL FAR PASCAL AboutDlgProc(HWND, WORD, WORD, LONG);
  38.  
  39. BOOL FAR PASCAL HelloDlgProc(HWND, WORD, WORD, LONG);
  40. long FAR PASCAL WndProc(HWND, unsigned, WORD, LONG);
  41. BOOL            InitInstance(HANDLE);
  42.  
  43. int DoFileOpenDlg(HANDLE, HANDLE, POFSTRUCT);
  44. BOOL FAR PASCAL FileOpenDlgProc(HWND, WORD, WORD, LONG);
  45. LPSTR lstrchr (LPSTR str, char ch);
  46.  
  47. /* The main procedure. This is called by Windows when your program is run. */
  48. /* hInstance is the handle for the current instance of the program         */
  49. /* hPrevInst is the handle for the last instance of the program to run     */
  50. /* CmdLine is a pointer to a string of whatever command line params came in*/
  51. /* nCmd is the specifies the applications appearance.                      */
  52.  
  53. int FAR PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInst, LPSTR CmdLine,
  54.                                                                      int nCmd)
  55. {
  56. MSG   msg;                                /* Message */
  57. HWND  hWnd;                       /* Our Window Handle */
  58. HMENU hMenu;
  59.  
  60. if (!hPrevInst)                         /* If no instance already exists, */
  61.    {
  62.     if (!InitInstance(hInstance))       /* try initializing instance */
  63.         return FALSE;                     /* No dice, it failed */
  64.     }
  65.  
  66. hInst = hInstance;
  67.     
  68. hWndMain = CreateWindow(szAppName,              /* Window Class */
  69.                               "Hello World Program",  /* Window caption */
  70.                               WS_OVERLAPPEDWINDOW,    /* Overlapped style */
  71.                               CW_USEDEFAULT,          /* Use defaults for the */
  72.                               CW_USEDEFAULT,          /* X & Y Positions and  */
  73.                               CW_USEDEFAULT,          /* X & Y sizes */
  74.                               CW_USEDEFAULT,          
  75.                               NULL,                   /* Parent Window */
  76.                               NULL,
  77.                               hInstance,              /* Instance */
  78.                               NULL);                  /* Creation Params */
  79.  
  80. if (hWndMain == NULL)
  81.    return 0;
  82.  
  83. hMenu = GetSystemMenu(hWndMain, FALSE);
  84. hAlternateMenu = LoadMenu(hInstance, "Hello2");
  85.  
  86. AppendMenu(hMenu, MF_SEPARATOR, 0, NULL);
  87. AppendMenu(hMenu, MF_STRING,    IDM_HELPABOUT, "About...");
  88. AppendMenu(hMenu, MF_STRING,    IDM_SETUP, "Setup...");
  89.  
  90. ShowWindow(hWndMain, nCmd);
  91. UpdateWindow(hWndMain);
  92.     
  93. while (GetMessage(&msg, NULL, 0, 0))
  94.    {
  95.     TranslateMessage(&msg);
  96.     DispatchMessage(&msg);
  97.    }
  98.     
  99. return msg.wParam;
  100. }                                       /* int FAR PASCAL WinMain */
  101.  
  102. BOOL InitInstance (HANDLE hInstance)
  103. {
  104. WNDCLASS wc;                            /* Window class structure */
  105.  
  106. wc.lpszMenuName  = szAppName;
  107. wc.lpszClassName = szAppName;
  108. wc.hbrBackground = GetStockObject(WHITE_BRUSH);  /* White bkgrnd */
  109. wc.hInstance     = hInstance;
  110. wc.style         = CS_VREDRAW | CS_HREDRAW;
  111. wc.lpfnWndProc   = WndProc;              /* Name of proc to handle window */
  112. wc.hCursor       = LoadCursor(NULL, IDC_ARROW);  /* Arrow Cursor */
  113. wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);  /* Generic Icon */
  114. wc.cbClsExtra    = 0;                /* no extras */
  115. wc.cbWndExtra    = 0;                /* No Extras */
  116.  
  117. return (RegisterClass(&wc)); /* Let's register that class */
  118. }                                       /* BOOL InitInstance */
  119.  
  120. /* This is our main windows procedure. It receives messages in message, then,
  121. depending on what the message is, we react accordingly
  122. */
  123.  
  124. long FAR PASCAL WndProc(HWND hWnd, unsigned message, WORD wParam, LONG lParam)
  125. {
  126. HDC            hdc;
  127. PAINTSTRUCT    ps;
  128. RECT           rect;
  129. HMENU          hMenu;
  130. static FARPROC lpfnHelloDlgProc, lpfnAboutDlgProc;
  131. char           OutMsg[25];
  132.  
  133. lstrcpy(OutMsg, "");
  134.  
  135. switch (message)
  136.    {
  137.    case WM_CREATE :
  138.       hMainMenu = GetMenu(hWnd);
  139.       InitSettings = GetPrivateProfileInt("Hello", "Setup", 1, "Hello.ini");
  140.       InitSettings = InitSettings ? IDM_HELLO : IDM_GOODBYE;
  141.       hdc = GetDC(hWnd);
  142.          InvalidateRect(hWnd, NULL, TRUE);
  143.       ReleaseDC(hWnd, hdc);
  144.  
  145. /*----------------------------------------------------------------------------
  146.   fall through to WM_PAINT...
  147. ----------------------------------------------------------------------------*/
  148.    case WM_PAINT :
  149.       hdc = BeginPaint(hWnd,&ps);        /* returns pointer to hdc */
  150.       GetClientRect(hWnd, &rect);
  151. /*
  152.   -1 tells the DrawText function to calculate length of string based on
  153.   NULL-termination
  154. */
  155.       DrawText(hdc, (InitSettings == IDM_HELLO) ? "Hello Windows!" : "Goodbye Windows!", -1,
  156.                                    &rect, DT_SINGLELINE|DT_CENTER|DT_VCENTER);
  157.       EndPaint(hWnd,&ps);
  158.       return 0;
  159.  
  160.    case WM_SYSCOMMAND :
  161.       switch (wParam)
  162.          {
  163.          case IDM_SETUP :
  164.             lpfnHelloDlgProc = MakeProcInstance(HelloDlgProc, hInst);
  165.             DialogBox(hInst, "Hello", hWnd, lpfnHelloDlgProc);
  166.             FreeProcInstance(lpfnHelloDlgProc);
  167.             return 0;
  168.  
  169.          case IDM_HELPABOUT :
  170.             lpfnAboutDlgProc = MakeProcInstance(AboutDlgProc, hInst);
  171.             DialogBox(hInst, "About", hWnd, lpfnAboutDlgProc);
  172.             FreeProcInstance(lpfnAboutDlgProc);
  173.             return 0;
  174.          }
  175.  
  176.       break;
  177.  
  178.    case WM_COMMAND :
  179.       hMenu = GetMenu(hWnd);
  180.       switch (wParam)
  181.          {
  182.       case IDM_FILENEW :
  183.          lstrcpy(OutMsg, "IDM_FILENEW");
  184.          break;
  185.  
  186.       case IDM_FILEOPEN :
  187.          if (DoFileOpenDlg(hInst, hWnd, &of))
  188.             lstrcpy(OutMsg, (char far *)pof->szPathName);
  189.          break;
  190.  
  191.       case IDM_FILESAVE :
  192.          lstrcpy(OutMsg, "IDM_FILESAVE");
  193.          break;
  194.  
  195.       case IDM_FILESAVEAS :
  196.          lstrcpy(OutMsg, "IDM_FILESAVEAS");
  197.          break;
  198.  
  199.       case IDM_FILEPRINT :
  200.          lstrcpy(OutMsg, "IDM_FILEPRINT");
  201.          break;
  202.  
  203.       case IDM_FILEPRINTPREV :
  204.          lstrcpy(OutMsg, "IDM_FILEPRINTPREV");
  205.          break;
  206.  
  207.       case IDM_FILEPRINTSETUP :
  208.          lstrcpy(OutMsg, "IDM_FILEPRINTSETUP");
  209.          break;
  210.  
  211.       case IDM_FILEEXIT :
  212.          lstrcpy(OutMsg, "IDM_FILEEXIT");
  213.          break;
  214.  
  215.  
  216.       case IDM_EDITUNDO :
  217.          lstrcpy(OutMsg, "IDM_EDITUNDO");
  218.          break;
  219.  
  220.       case IDM_EDITREPEAT :
  221.          lstrcpy(OutMsg, "IDM_EDITREPEAT");
  222.          break;
  223.  
  224.       case IDM_EDITCUT :
  225.          lstrcpy(OutMsg, "IDM_EDITCUT");
  226.          break;
  227.  
  228.       case IDM_EDITCOPY :
  229.          lstrcpy(OutMsg, "IDM_EDITCOPY");
  230.          break;
  231.  
  232.       case IDM_EDITPASTE :
  233.          lstrcpy(OutMsg, "IDM_EDITPASTE");
  234.          break;
  235.  
  236.       case IDM_EDITCLEAR :
  237.          lstrcpy(OutMsg, "IDM_EDITCLEAR");
  238.          break;
  239.  
  240.       case IDM_EDITPAST :
  241.          lstrcpy(OutMsg, "IDM_EDITPAST");
  242.          break;
  243.  
  244.  
  245.       case IDM_SAMPLEDLG :
  246.          lstrcpy(OutMsg, "IDM_SAMPLEDLG");
  247.          break;
  248.  
  249.  
  250.       case IDM_HELPINDX :
  251.          lstrcpy(OutMsg, "IDM_HELPINDX");
  252.          break;
  253.  
  254.       case IDM_HELPKBD :
  255.          lstrcpy(OutMsg, "IDM_HELPKBD");
  256.          break;
  257.  
  258.       case IDM_HELPCMDS :
  259.          lstrcpy(OutMsg, "IDM_HELPCMDS");
  260.          break;
  261.  
  262.       case IDM_HELPPROCS :
  263.          lstrcpy(OutMsg, "IDM_HELPPROCS");
  264.          break;
  265.  
  266.       case IDM_HELPUSING :
  267.          lstrcpy(OutMsg, "IDM_HELPUSING");
  268.          break;
  269.  
  270.       case IDM_HELPABOUT :
  271.          lpfnAboutDlgProc = MakeProcInstance(AboutDlgProc, hInst);
  272.          DialogBox(hInst, "About", hWnd, lpfnAboutDlgProc);
  273.          FreeProcInstance(lpfnAboutDlgProc);
  274.          return 0;
  275.  
  276.       case IDM_ALTERNATE :
  277.          SetMenu(hWnd, hAlternateMenu);
  278.          return 0;
  279.  
  280.       case IDM_ALTONE : 
  281.          CheckMenuItem(hMenu, IDM_ALTONE, MF_CHECKED);
  282.          CheckMenuItem(hMenu, IDM_ALTTWO, MF_UNCHECKED);
  283.          return 0;
  284.  
  285.       case IDM_ALTTWO : 
  286.          CheckMenuItem(hMenu, IDM_ALTTWO, MF_CHECKED);
  287.          CheckMenuItem(hMenu, IDM_ALTONE, MF_UNCHECKED);
  288.          return 0;
  289.  
  290.       case IDM_ALTGRAY : 
  291.          EnableMenuItem(hMenu, IDM_ALTGRAY, MF_GRAYED);
  292.          EnableMenuItem(hMenu, IDM_ALTUNGRAY, MF_ENABLED);
  293.          return 0;
  294.  
  295.       case IDM_ALTUNGRAY : 
  296.          EnableMenuItem(hMenu, IDM_ALTUNGRAY, MF_GRAYED);
  297.          EnableMenuItem(hMenu, IDM_ALTGRAY, MF_ENABLED);
  298.          return 0;
  299.  
  300.       case IDM_ALTRESTORE : 
  301.          SetMenu(hWnd, hMainMenu);
  302.          return 0;
  303.  
  304.       case WM_DESTROY :
  305.           PostQuitMessage(0);
  306.           return 0;
  307.           }
  308.  
  309.    if (lstrlen(OutMsg) != 0)
  310.       {
  311.       MessageBox(hWnd, OutMsg, szAppName, MB_ICONEXCLAMATION | MB_OK);
  312.       return 0;
  313.       }
  314.  
  315.    }
  316. return DefWindowProc(hWnd, message, wParam, lParam);
  317. }                                       /* long FAR PASCAL WndProc */
  318.  
  319. /*============================================================================
  320.   HelloDlgProc -- 
  321.  
  322.   File      : Test.C
  323.  
  324.   Prototype : BOOL FAR PASCAL HelloDlgProc(HWND, WORD, WORD, LONG);
  325.  
  326.   Call      : HelloDlgProc(hDlg, message, wParam, lParam);
  327.  
  328.   Library   : 
  329.  
  330.   24 April 1993 - dlcampbell
  331.   (c) 1993 WynApse
  332. ----------------------------------------------------------------------------*/
  333. BOOL FAR PASCAL HelloDlgProc (HWND hDlg, WORD message, WORD wParam,
  334.                                                                   LONG lParam)
  335. {
  336. switch (message)
  337.    {
  338.    case WM_INITDIALOG :
  339.       CheckRadioButton(hDlg, IDM_HELLO, IDM_GOODBYE, InitSettings);
  340.       return TRUE;
  341.  
  342.    case WM_COMMAND :
  343.       switch (wParam)
  344.          {
  345.          case IDM_HELLO : 
  346.                WritePrivateProfileString("Hello", "Setup", "1", "Hello.ini");
  347.             InitSettings = wParam;
  348.             break;
  349.  
  350.          case IDM_GOODBYE : 
  351.                WritePrivateProfileString("Hello", "Setup", "0", "Hello.ini");
  352.             InitSettings = wParam;
  353.             break;
  354.  
  355.          case IDOK :
  356.             EndDialog(hDlg, wParam);
  357.             return TRUE;
  358.  
  359.          }
  360.       break;
  361.  
  362.    }
  363. return FALSE;
  364. }                                       /* HelloDlgProc */
  365. /*============================================================================
  366.   AboutDlgProc
  367.  
  368.   File      : ExitWin.C
  369.  
  370.   Prototype : 
  371.  
  372.   Call      : 
  373.  
  374.   Library   : 
  375.  
  376.   24 April 1993 - dlcampbell
  377.   (c) 1993 WynApse
  378. ----------------------------------------------------------------------------*/
  379. BOOL FAR PASCAL AboutDlgProc (HWND hDlg, WORD message, WORD wParam,
  380.                                                                   LONG lParam)
  381. {
  382. char szBuffer[100];
  383.  
  384. switch (message)
  385.    {
  386.    case WM_INITDIALOG :
  387.       wsprintf(szBuffer, "%s at %s", (LPSTR) __DATE__, (LPSTR) __TIME__);
  388.       SetWindowText(GetDlgItem(hDlg, ID_VERSION), szBuffer);
  389.       return TRUE;
  390.  
  391.    case WM_COMMAND :
  392.       switch (wParam)
  393.          {
  394.          case IDOK :
  395.          case IDCANCEL :
  396.             if (HIWORD(lParam) == BN_CLICKED)
  397.                EndDialog(hDlg, wParam);
  398.             return TRUE;
  399.  
  400.          default :
  401.             return TRUE;
  402.          }
  403.  
  404.    default :
  405.       return FALSE;
  406.    }
  407. }                                       /* AboutDlgProc */
  408. /*============================================================================
  409.   DoFileOpenDlg --
  410.  
  411.   File      : FileDlg.C
  412.  
  413.   Prototype : int DoFileOpenDlg(HANDLE, HANDLE, char *, char *, WORD,
  414.                                 char *, POFSTRUCT)
  415.  
  416.   Call      : DoFileOpenDlg(hInst, hWnd, szFileSpecIn, szDefExtIn, WORD,
  417.                             szFileNameOut, pofIn)
  418.  
  419.   Library   : 
  420.  
  421.   24 April 1993 - dlcampbell
  422.   (c) 1993 WynApse
  423. ----------------------------------------------------------------------------*/
  424. int DoFileOpenDlg(HANDLE hInst, HANDLE hWnd, POFSTRUCT pofIn)
  425. {
  426. FARPROC    lpfnFileOpenDlgProc;
  427. int        iReturn;
  428.  
  429. pof = pofIn;
  430. lstrcpy(szFileSpec, "*.EXE");
  431. lstrcpy(szDefExt,   "EXE");
  432. wFileAttr = DDL_DRIVES | DDL_DIRECTORY; /* 0x4010 */
  433.  
  434. lpfnFileOpenDlgProc = MakeProcInstance((FARPROC)FileOpenDlgProc, hInst);
  435. iReturn = DialogBox(hInst, "FileOpen", hWnd, lpfnFileOpenDlgProc);
  436. FreeProcInstance(lpfnFileOpenDlgProc);
  437.  
  438. return iReturn;
  439. }                                       /* DoFileOpenDlg */
  440.  
  441. /*============================================================================
  442.   FileOpenDlgProc
  443.  
  444.   File      : FileDlg.C
  445.  
  446.   Prototype : BOOL FAR PASCAL FileOpenDlgProc(HWND, WORD, WORD, LONG);
  447.  
  448.   Call      : WndProc(hWnd, message, wParam, lParam);
  449.  
  450.   Library   :
  451.  
  452.   24 April 1993 - dlcampbell
  453. ----------------------------------------------------------------------------*/
  454. BOOL FAR PASCAL FileOpenDlgProc(HWND hDlg, WORD message,
  455.                                 WORD wParam, LONG lParam)
  456. {
  457. char    cLastChar;
  458. short   nEditLen;
  459.  
  460. switch (message)
  461.    {
  462.    case WM_INITDIALOG :
  463.       SendDlgItemMessage(hDlg, IDD_FNAME, EM_LIMITTEXT, 80, 0L);
  464.       DlgDirList(hDlg, szFileSpec, IDD_FLIST, IDD_FPATH, wFileAttr);
  465.       SetDlgItemText(hDlg, IDD_FNAME, szFileSpec);
  466.       return TRUE;
  467.  
  468.    case WM_COMMAND :
  469.       switch (wParam)
  470.          {
  471.          case IDD_FLIST :
  472.             switch (HIWORD(lParam))
  473.                {
  474.                case LBN_SELCHANGE :
  475.                   if (DlgDirSelect(hDlg, szFileName, IDD_FLIST))
  476.                      lstrcat(szFileName, szFileSpec);
  477.                   SetDlgItemText(hDlg, IDD_FNAME, szFileName);
  478.                   return TRUE;
  479.  
  480.                case LBN_DBLCLK :
  481.                   if (DlgDirSelect(hDlg, szFileName, IDD_FLIST))
  482.                      {
  483.                      lstrcat(szFileName, szFileSpec);
  484.                      DlgDirList(hDlg, szFileName, IDD_FLIST, IDD_FPATH, wFileAttr);
  485.                        SetDlgItemText(hDlg, IDD_FNAME, szFileSpec);
  486.                      }
  487.                   else
  488.                      {
  489.                      SetDlgItemText(hDlg, IDD_FNAME, szFileName);
  490.                      SendMessage(hDlg, WM_COMMAND, IDOK, 0L);
  491.                      }
  492.                   return TRUE;
  493.                }
  494.             break;
  495.  
  496.          case IDD_FNAME :
  497.             if (HIWORD(lParam) == EN_CHANGE)
  498.                EnableWindow(GetDlgItem(hDlg, IDOK),
  499.                  (BOOL) SendMessage(LOWORD(lParam), WM_GETTEXTLENGTH, 0, 0L));
  500.             return TRUE;
  501.  
  502.          case IDOK :
  503.             GetDlgItemText(hDlg, IDD_FNAME, szFileName, 80);
  504.             nEditLen = lstrlen(szFileName);
  505.             cLastChar = *AnsiPrev(szFileName, szFileName + nEditLen);
  506.  
  507.             if (cLastChar == '\\' || cLastChar == ':')
  508.                lstrcat(szFileName, szFileSpec);
  509.  
  510.             if (lstrchr(szFileName, '*') || lstrchr(szFileName, '?'))
  511.                {
  512.                if (DlgDirList(hDlg, szFileName, IDD_FLIST, IDD_FPATH, wFileAttr))
  513.                   {
  514.                   lstrcpy(szFileSpec, szFileName);
  515.                   SetDlgItemText(hDlg, IDD_FNAME, szFileSpec);
  516.                   }
  517.                else
  518.                   MessageBeep(0);
  519.       
  520.                return TRUE;
  521.                }
  522.             lstrcat(lstrcat(szFileName, "\\"), szFileSpec);
  523.  
  524.             if (DlgDirList(hDlg, szFileName, IDD_FLIST, IDD_FPATH, wFileAttr))
  525.                {
  526.                lstrcpy(szFileSpec, szFileName);
  527.                SetDlgItemText(hDlg, IDD_FNAME, szFileSpec);
  528.                return TRUE;
  529.                }
  530.             szFileName[nEditLen] = '\0';
  531.  
  532.             if (-1 == OpenFile(szFileName, pof, OF_READ | OF_EXIST))
  533.                {
  534.                lstrcat(szFileName, szDefExt);
  535.                if (-1 == OpenFile(szFileName, pof, OF_READ | OF_EXIST))
  536.                   {
  537.                   MessageBeep(0);
  538.                   return TRUE;
  539.                   }
  540.                }
  541.  
  542.             EndDialog(hDlg, TRUE);
  543.             return TRUE;
  544.  
  545.          case IDCANCEL :
  546.             EndDialog(hDlg, FALSE);
  547.             return TRUE;
  548.          }
  549.    }
  550. return FALSE;
  551. }                                       /* FileOpenDlgProc */
  552.  
  553. /*============================================================================
  554.   lstrchr --
  555.  
  556.   File      : Filedlg.C
  557.  
  558.   Prototype : LPSTR lstrchr(char, char);
  559.  
  560.   Call      : lstrchr(str, ch);
  561.  
  562.   Library   : 
  563.  
  564.   24 April 1993 - dlcampbell
  565. ----------------------------------------------------------------------------*/
  566. LPSTR lstrchr(LPSTR str, char ch)
  567. {
  568. while (*str)
  569.    {
  570.    if (ch == *str)
  571.       return str;
  572.    str = AnsiNext(str);
  573.    }
  574. return NULL;
  575. }                                       /* lstrchr */
  576.